<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->
    <input
      type="file"
      hidden
      ref="fileIpt"
      @change="onFileChange"
    >
    <!-- 个人信息 -->
    <van-cell
      class="avatar-cell"
      title="头像"
      is-link
      center
      @click="$refs.fileIpt.click()"
    >
      <van-image
        class="avatar"
        round
        fit="cover"
        :src="user.photo"
      />
    </van-cell>
    <van-cell
      title="昵称"
      :value="user.name"
      is-link
      @click="isNamePopShow=true"
    />
    <van-cell
      title="性别"
      :value="user.gender === 0 ? '男' : '女'"
      is-link
      @click="isSexPopShow=true"
    />
    <van-cell
      title="生日"
      :value="user.birthday"
      is-link
      @click="isBirthdayPopShow=true"
    />
    <!-- /个人信息 -->

    <!-- 弹出层 -->
    <!-- 修改昵称 -->
    <van-popup
      class="pop-toast"
      v-model="isNamePopShow"
      position="bottom"
      :style="{ height: '90%' }"
    >
    <UserName
      v-if="isNamePopShow"
      @closePop="isNamePopShow=false"
      v-model="user.name"
    />
    </van-popup>
    <!-- 修改性别 -->
    <van-popup
      class="pop-toast"
      v-model="isSexPopShow"
      position="bottom"
      :style="{ height: '40%' }"
    >
     <UserSex
     v-model="user.gender"
      @closePop="isSexPopShow=false"
     />
    </van-popup>
    <!-- 修改生日 -->
    <van-popup
      class="pop-toast"
      v-model="isBirthdayPopShow"
      position="bottom"
      :style="{ height: '40%' }"
    >
     <UserBirthday
     v-model="user.birthday"
      @closePop="isBirthdayPopShow=false"
     />
    </van-popup>
    <!-- 修改头像 -->
      <van-popup
      class="pop-toast"
      v-model="isPhotoPopShow"
      position="bottom"
      :style="{ height: '100%' }"
    >
     <UserPhoto
     v-model="user.photo"
     :imgUrl="imgUrl"
      @closePop="isPhotoPopShow=false"
      @updateUrl="user.photo = $event"
     />
    </van-popup>
  </div>
</template>

<script>
import { getOwnProfile } from '@/api/user'
import UserName from './components/user-name'
import UserSex from './components/user-sex'
import UserBirthday from './components/user-birthday'
import UserPhoto from './components/user-photo'

export default {
  name: 'UserProfile',
  components: {
    UserName, UserSex, UserBirthday, UserPhoto
  },
  props: {},
  data () {
    return {
      user: {},
      isNamePopShow: false,
      isSexPopShow: false,
      isBirthdayPopShow: false,
      isPhotoPopShow: false,
      imgUrl: null
    }
  },
  created () {
    this.loadUserInfo()
  },
  methods: {
    async loadUserInfo () {
      try {
        const { data } = await getOwnProfile()
        this.user = data.data
      } catch (err) {
        this.$toast('获取个人信息失败，稍后重试')
      }
    },
    onFileChange () {
      const file = this.$refs.fileIpt.files[0]
      this.imgUrl = window.URL.createObjectURL(file)
      this.isPhotoPopShow = true
      this.$refs.fileIpt.value = '' // 为了解决两次选一样的图片弹层不触发
    }
  }
}
</script>

<style scoped lang="less">
.avatar-cell {
    .van-cell__value {
      display: flex;
      flex-direction: row-reverse;
      .van-image {
        width: 80px;
        height: 80px;
      }
    }
}
/deep/.user-profile {
  .avatar-cell {
    .van-cell__value {
      display: flex;
      flex-direction: row-reverse;
      .van-image {
        width: 100px !important;
        height: 100px !important;
      }
    }
    .avatar {
      width: 60px;
      height: 60px;
    }
  }
}
</style>
